package com.keda.listener;

import com.keda.model.BigType;
import com.keda.model.News;
import com.keda.model.Notice;
import com.keda.model.Tag;
import com.keda.service.IndexService;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // Servlet容器启动加载上下文
        System.out.println("===============容器启动加载公共数据。。。。。。。。。");

        IndexService indexService = new IndexService();

        // 获取分类数据
        List<BigType> bigTypeList = indexService.findBigType();
        // 把bigTypeList放入Request中，方便jsp进行读取展示
        servletContextEvent.getServletContext().setAttribute("bigTypes", bigTypeList);

        // 获取标签数据
        List<Tag> tags = indexService.findTags();
        servletContextEvent.getServletContext().setAttribute("tags", tags);

        // 完成广告、公告的列表编写
        List<Notice> notices = indexService.findLatestNotice();
        servletContextEvent.getServletContext().setAttribute("notices", notices);
        List<News> news = indexService.findLatestNews();
        servletContextEvent.getServletContext().setAttribute("news", notices);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

        // Servlet容器销毁
        System.out.println("===============容器销毁拉。。。。。。。。。");
    }
}
